home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / COMLIN.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  7KB  |  333 lines

  1. /* -*-C-*-
  2.  
  3. Copyright (c) 1987-92 Massachusetts Institute of Technology
  4.  
  5. This material was developed by the Scheme project at the Massachusetts
  6. Institute of Technology, Department of Electrical Engineering and
  7. Computer Science.  Permission to copy this software, to redistribute
  8. it, and to use it for any purpose is granted, subject to the following
  9. restrictions and understandings.
  10.  
  11. 1. Any copy made of this software must include this copyright notice
  12. in full.
  13.  
  14. 2. Users of this software agree to make their best efforts (a) to
  15. return to the MIT Scheme project any improvements or extensions that
  16. they make, so that these may be included in future releases; and (b)
  17. to inform MIT of noteworthy uses of this software.
  18.  
  19. 3. All materials developed as a consequence of the use of this
  20. software shall duly acknowledge such use, in accordance with the usual
  21. standards of acknowledging credit in academic research.
  22.  
  23. 4. MIT has made no warrantee or representation that the operation of
  24. this software will be error-free, and MIT is under no obligation to
  25. provide any services, by way of maintenance, update, or otherwise.
  26.  
  27. 5. In conjunction with products arising from the use of this material,
  28. there shall be no use of the name of the Massachusetts Institute of
  29. Technology nor of any adaptation thereof in any advertising,
  30. promotional, or sales literature without prior written consent from
  31. MIT in each case. */
  32.  
  33. /* $Header: /scheme/src/microcode/RCS/comlin.c,v 1.7 1992/01/15 21:04:42 jinx Exp $
  34.  *
  35.  * This file contains the scheme command parser.
  36.  *
  37.  */
  38.  
  39. #include <stdio.h>
  40. #ifndef toupper
  41. #include <ctype.h>
  42. #endif
  43.  
  44. #include "comlin.h"
  45.  
  46. /* Some string utilities */
  47.  
  48. char *
  49. DEFUN (remove_initial_substring, (sub, str),
  50.        register char * sub
  51.        AND register char * str)
  52. {
  53.   for ( ; *sub != '\0'; sub++, str++)
  54.   {
  55.     if (*sub != *str)
  56.     {
  57.       return ((char *) NULL);
  58.     }
  59.   }
  60.   return (str);
  61. }
  62.  
  63. boolean
  64. DEFUN (STREQUAL, (s1, s2),
  65.        register char * s1
  66.        AND register char * s2)
  67. {
  68.   for ( ; *s1 != '\0'; s1++, s2++)
  69.   {
  70.     if (toupper(*s1) != toupper(*s2))
  71.     {
  72.       return (false);
  73.     }
  74.   }
  75.   return (*s2 == '\0');
  76. }
  77.  
  78. /* Usage information */
  79.  
  80. void
  81. DEFUN (print_usage_and_exit, (options, val),
  82.        struct keyword_struct * options
  83.        AND int val)
  84. {
  85.   register int i;
  86.  
  87.   fprintf(stderr, "usage: %s", program_name);
  88.  
  89.   if ((options[0].type_tag) == LAST_KYWRD)
  90.   {
  91.     fprintf(stderr, "\n");
  92.     exit(val);
  93.   }
  94.  
  95.   fprintf(stderr, " [args]\n");
  96.   fprintf(stderr, "    where args are as follows:\n");
  97.  
  98.   for (i = 0;
  99.        ((options[i].type_tag) != LAST_KYWRD);
  100.        i++)
  101.   {
  102.     switch (options[i].type_tag)
  103.     {
  104.       case BOOLEAN_KYWRD:
  105.     fprintf(stderr, "        %s={true,false}\n",
  106.         options[i].keyword);
  107.     break;
  108.  
  109.       case INT_KYWRD:
  110.       case DOUBLE_KYWRD:
  111.     fprintf(stderr, "        %s=%s\n",
  112.         options[i].keyword, options[i].format);
  113.     break;
  114.  
  115.       case STRING_KYWRD:
  116.     fprintf(stderr, "        %s=%%s\n",
  117.         options[i].keyword);
  118.     break;
  119.     }
  120.   }
  121.   exit(val);
  122. }
  123.  
  124. void
  125. DEFUN (supply, (options, j),
  126.        struct keyword_struct * options
  127.        AND int j)
  128. {
  129.   if (options[j].supplied_p != ((boolean *) NULL))
  130.   {
  131.     if (*(options[j].supplied_p))
  132.     {
  133.       fprintf(stderr,
  134.           "parse_keywords: Repeated keyword: %s\n",
  135.           options[j].keyword);
  136.       print_usage_and_exit(&options[0], 1);
  137.     }
  138.     else
  139.     {
  140.       *(options[j].supplied_p) = true;
  141.     }
  142.   }
  143.   return;
  144. }
  145.  
  146. char * program_name;
  147.  
  148. /* This parser assumes that no keyword is an initial
  149.    substring of another.
  150.  */
  151.  
  152. void
  153. DEFUN (parse_keywords,
  154.        (argc, argv, options, allow_others_p),
  155.        int argc
  156.        AND char **argv
  157.        AND struct keyword_struct * options
  158.        AND boolean allow_others_p)
  159. {
  160.   register int i, j, length;
  161.   char *argument;
  162.  
  163.   program_name = argv[0];
  164.   argv += 1;
  165.   argc -= 1;
  166.  
  167.   /* Initialize defaults */
  168.  
  169.   for (length = 0;
  170.        ((options[length].type_tag) != LAST_KYWRD);
  171.        length++)
  172.   {
  173.     if (options[length].supplied_p != ((boolean *) NULL))
  174.     {
  175.       *(options[length].supplied_p) = false;
  176.     }
  177.  
  178.     switch (options[length].type_tag)
  179.     {
  180.       case BOOLEAN_KYWRD:
  181.         if (options[length].format != BFRMT)
  182.     {
  183.       fprintf(stderr,
  184.           "parse_keywords: format (%s) for boolean keyword %s\n",
  185.           options[length].format,
  186.           options[length].keyword);
  187.       exit(1);
  188.     }
  189.     break;
  190.  
  191.       case INT_KYWRD:
  192.     break;
  193.  
  194.       case DOUBLE_KYWRD:
  195.     break;
  196.  
  197.       case STRING_KYWRD:
  198.         if (options[length].format != SFRMT)
  199.     {
  200.       fprintf(stderr,
  201.           "parse_keywords: format (%s) for string keyword %s\n",
  202.           options[length].format,
  203.           options[length].keyword);
  204.       exit(1);
  205.     }
  206.     break;
  207.  
  208.       default:
  209.         fprintf(stderr, "parse_keywords: bad type %d\n",
  210.         options[length].type_tag);
  211.     exit(1);
  212.     }
  213.   }
  214.  
  215.   for (i = 0; i < argc; i++)
  216.   {
  217.     for (j = 0; j < length; j++)
  218.     {
  219.       argument = remove_initial_substring(options[j].keyword,argv[i]);
  220.       if (argument != ((char *) NULL))
  221.       {
  222.     switch (options[j].type_tag)
  223.     {
  224.  
  225.       case BOOLEAN_KYWRD:
  226.       {
  227.         boolean value;
  228.  
  229.         if (*argument != '\0')
  230.         {
  231.           if (*argument != '=')
  232.           {
  233.         fprintf(stderr,
  234.             "parse_keywords: unrecognized parameter: %s\n",
  235.             argv[i]);
  236.         print_usage_and_exit(&options[0], 1);
  237.           }
  238.           else
  239.           {
  240.         argument = &argument[1];
  241.         if (STREQUAL(argument,"t") || STREQUAL(argument,"true"))
  242.         {
  243.           value = true;
  244.         }
  245.         else if (STREQUAL(argument,"f") ||
  246.              STREQUAL(argument,"false") ||
  247.              STREQUAL(argument,"nil"))
  248.         {
  249.           value = false;
  250.         }
  251.         else
  252.         {
  253.           fprintf(stderr,
  254.               "parse_keywords: Invalid boolean value: %s\n",
  255.               argv[i]);
  256.           print_usage_and_exit(&options[0], 1);
  257.         }
  258.           }
  259.         }
  260.         else
  261.         {
  262.           value = true;
  263.         }
  264.         supply(options, j);
  265.         *(BOOLEAN_LVALUE(options[j])) = value;
  266.         break;
  267.       }
  268.  
  269.       case INT_KYWRD:
  270.         if (*argument != '=')
  271.         {
  272.           {
  273.         fprintf(stderr,
  274.             "parse_keywords: %s: %s\n",
  275.             ((*argument == '\0')    ?
  276.              "missing integer value"    :
  277.              "unrecognized parameter"),
  278.             argv[i]);
  279.         print_usage_and_exit(&options[0], 1);
  280.           }
  281.         }
  282.         supply(options, j);
  283.         sscanf(&argument[1], options[j].format, INT_LVALUE(options[j]));
  284.         break;
  285.  
  286.       case DOUBLE_KYWRD:
  287.         if (*argument != '=')
  288.         {
  289.           {
  290.         fprintf(stderr,
  291.             "parse_keywords: %s: %s\n",
  292.             ((*argument == '\0')        ?
  293.              "missing floating point value"    :
  294.              "unrecognized parameter"),
  295.             argv[i]);
  296.         print_usage_and_exit(&options[0], 1);
  297.           }
  298.         }
  299.         supply(options, j);
  300.         sscanf(&argument[1], options[j].format, DOUBLE_LVALUE(options[j]));
  301.         break;
  302.  
  303.       case STRING_KYWRD:
  304.         if (*argument != '=')
  305.         {
  306.           {
  307.         fprintf(stderr,
  308.             "parse_keywords: %s: %s\n",
  309.             ((*argument == '\0')    ?
  310.              "missing string value"    :
  311.              "unrecognized parameter"),
  312.             argv[i]);
  313.         print_usage_and_exit(&options[0], 1);
  314.           }
  315.         }
  316.         supply(options, j);
  317.         *(STRING_LVALUE(options[j])) = &argument[1];
  318.         break;
  319.       }
  320.     break;
  321.       }
  322.     }
  323.     if ((j >= length) && (!allow_others_p))
  324.     {
  325.       fprintf(stderr,
  326.           "parse_keywords: unrecognized parameter: %s\n",
  327.           argv[i]);
  328.       print_usage_and_exit(&options[0], 1);
  329.     }
  330.   }
  331.   return;
  332. }
  333.